package com.ht.medicaltreatment.fragment.haocai2

import android.content.Intent
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.UiMessageUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseFragment
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.activity.haocai2.PrepareConsumableActivity
import com.ht.medicaltreatment.activity.haocai2.SendOffActivity
import com.ht.medicaltreatment.bean.ApplyRecordPage
import com.ht.medicaltreatment.view.InputEditTextDialog
import com.ht.medicaltreatment.viewmodel.ConsumableRecordViewModel
import kotlinx.android.synthetic.main.fragment_consumable_record_layout.recyclerView

//耗材申请记录的Fragment界面
class ConsumableRecordFragment:BaseFragment(), OnItemChildClickListener {
    override fun getLayoutResId() = R.layout.fragment_consumable_record_layout

    private val model by viewModels<ConsumableRecordViewModel>()
    private var status = -1
    private var page = 1
    private var id = 0
    private lateinit var adapter:ConsumableRecordAdapter

    override fun init() {
        val position = arguments?.getInt("position")
        when(position){
            0 -> status = -1
            1 -> status = 1
            2 -> status = 0
            3 -> status = 2
        }

        id = arguments?.getInt("id")?:0

        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        adapter = ConsumableRecordAdapter()
        recyclerView.adapter = adapter
        adapter.setOnItemChildClickListener(this)

        UiMessageUtils.getInstance().addListener(121,object : UiMessageUtils.UiMessageCallback{
            override fun handleMessage(localMessage: UiMessageUtils.UiMessage) {
                initData()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    private fun initData() {
        showProgress()
        model.requestData(page,id,"$status"){
            dismissProgress()
            adapter.setAuthSign(it.authSign)
            adapter.setNewInstance(it.pageBean.pageList)
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as ApplyRecordPage
        val id = item.id
        when(view.id){
            R.id.btnNegaive -> {
                //拒绝
                InputEditTextDialog(requireContext()).showDialog {
                    showProgress()
                    model.approval(2, remark = it, id = id,{
                        dismissProgress()
                        initData()
                    })
                }
            }
            R.id.btnPositive -> {
                //同意
                showProgress()
                model.approval(1, id = id, onSuccess = {
                    dismissProgress()
                    initData()
                })
            }
            R.id.btnPrepare -> {
                //准备耗材
                val intent  = Intent(requireContext(), PrepareConsumableActivity::class.java)
                intent.putExtra("projectId",item.projectId)
                intent.putExtra("id",item.id)
                startActivity(intent)
            }
            R.id.btnSend -> {
                //寄出
                val intent = Intent(requireContext(), SendOffActivity::class.java)
                intent.putExtra("id",id)
                startActivity(intent)
            }
        }
    }
}